package com.pulse.common.utils.sor.make;

import com.pulse.common.utils.StringUtils;
import com.pulse.common.utils.sor.domain.SorInfo;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;

/**
 * @ClassName GenParamsMake
 * @Description
 * @Author 陆帅
 * @Date 2025/3/13 21:57
 */
public class GenParamsMake {

    public static final String GENERAL_PARAMS_BLOCK_ID = "GenParams\0";

    public static ByteBuffer make(SorInfo info) {

        ByteBuffer buffer = ByteBuffer.allocate(256);
        // 使用小端序
        buffer.order(ByteOrder.LITTLE_ENDIAN);

        // 写入块ID 10byte
        buffer.put(GENERAL_PARAMS_BLOCK_ID.getBytes());
        // 写入语言代码(EN = 英语)  2byte
        buffer.put("EN".getBytes());
        // 写入电缆ID ？byte
        String cableId = info.getGenParams().getCableID() == null ? "\0" : info.getGenParams().getCableID() + "\0";
        buffer.put(cableId.getBytes());
        // 写入光纤ID ?byte
        String fiberId = info.getGenParams().getFiberID() == null ? "\0" : info.getGenParams().getFiberID() + "\0";
        buffer.put(fiberId.getBytes());
        // 写入光纤类型
        String type = info.getGenParams().getFiberType() == null ? "" : info.getGenParams().getFiberType();
        int tp = 0;
        try {
            if (StringUtils.isNotEmpty(type)) {
                type = type.substring(2, 5);
                tp = Integer.parseInt(type);
            }
        } catch (Exception e) {

        }
        buffer.putShort((short) tp);

        // 写入波长
        try {
            buffer.putShort(Short.parseShort(info.getGenParams().getWavelength().split(" ")[0]));
        } catch (Exception e) {
            throw new RuntimeException("GenParams中波长解析失败");
        }

        // 写入起始位置LocaltionA
        String locationA = info.getGenParams().getLocationA() == null ? "\0" : info.getGenParams().getLocationA() + "\0";
        buffer.put(locationA.getBytes());
        // 写入结束位置LocaltionB
        String locationB = info.getGenParams().getLocationB() == null ? "\0" : info.getGenParams().getLocationB() + "\0";
        buffer.put(locationB.getBytes());

        // 写入电缆代码或纤芯类型
        String cableCode = info.getGenParams().getCableCodeOrFiberType() == null ? "\0" : info.getGenParams().getCableCodeOrFiberType() + "\0";
        buffer.put(cableCode.getBytes());
        // 写入测试条件
        String buildCondition = info.getGenParams().getBuildCondition() == null ? "UN (unknown)" : info.getGenParams().getBuildCondition();
        buffer.put(buildCondition.substring(0,2).getBytes());
        // 用户偏移量
        String userOffset = info.getGenParams().getUserOffset() == null ? "0" : info.getGenParams().getUserOffset();
        int userOffsetI = Integer.parseInt(userOffset);
        buffer.putInt(userOffsetI);
        // 用户偏移距离
        String userOffsetDistance = info.getGenParams().getUserOffsetDistance() == null ? "0" : info.getGenParams().getUserOffsetDistance();
        buffer.putInt(Integer.parseInt(userOffsetDistance));
        // 操作员名称
        String operator = info.getGenParams().getOperator() == null ? "\0":info.getGenParams().getOperator() + "\0";
        buffer.put(operator.getBytes());
        // 备注信息
        String comment = info.getGenParams().getComments() == null ? "\0":info.getGenParams().getComments() + "\0";
        buffer.put(comment.getBytes());

        return buffer;
    }
}
